package com.galen.wxRead.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
    /**
     * 取两个文本之间的文本值
     *
     * @param text  源文本 比如：欲取全文本为 12345
     * @param left  文本前面
     * @param right 后面文本
     * @return 返回 String
     */
    public static String getSubString(String text, String left, String right) {
        if (text.contains(left) && text.contains(right)) {
            String result = "";
            int zLen;
            if (left == null || left.isEmpty()) {
                zLen = 0;
            } else {
                zLen = text.indexOf(left);
                if (zLen > -1) {
                    zLen += left.length();
                } else {
                    zLen = 0;
                }
            }
            int yLen = text.indexOf(right, zLen);
            if (yLen < 0 || right == null || right.isEmpty()) {
                yLen = text.length();
            }
            result = text.substring(zLen, yLen);
            return result;
        } else {
            return "";
        }
    }

    /**
     * 利用正则表达式判断字符串是否是数字
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }
}
